#include <iostream>
#include <mysql/mysql.h>
#include <iomanip>

#include "mysqlDataBase.h"
using namespace std;

#if 0
int main()
{
    /* 初始化数据库句柄成功. */
    MYSQL *conn = mysql_init(NULL);
    if (conn == NULL)
    {
        cout << "mysql init failed." << endl;
        exit(-1);
    }

    /* 连接mysql服务器 */
    // const char *host = "127.0.0.1";
    const char *host = "localhost";
    const char *user = "root";
    const char *passwd = "1";
    const char *db = "musicDB";
    unsigned int port = 3306;
    if (mysql_real_connect(conn, host, user, passwd, db, port, NULL, 0) == NULL)
    {
        cout << "mysql connect error." << endl;
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }
    cout << "mysql connection success." << endl;

#if 0
    /* 插入数据 */
    const char *sql = "INSERT INTO userInfo (username,age) VALUES('shuaige',19);";

    if (mysql_query(conn, sql))
    {
        cout << "mysql query error." << endl;
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }
#endif

    /* 查询数据 */
    const char *sql = "select * from userInfo;";
    if (mysql_query(conn, sql))
    {
        cout << "mysql query error." << endl;
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }
    /* 从结果集里拿取数据 */
    MYSQL_RES *res = mysql_store_result(conn);
    if (res == NULL)
    {
        cout << "get store result error" << mysql_error(conn) << endl;
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }

    /* 获取几列数据 */
    unsigned int columns = mysql_num_fields(res);
    cout << "columns:" << columns << endl;

    /* 获取字段名称 */
    MYSQL_FIELD *fields = mysql_fetch_field(res);

    /* 打印所有列名的信息 */
    for (int idx = 0; idx < columns; idx++)
    {
        cout << fields[idx].name << "\t";
    }
    cout << endl;

    // unsigned int rows = mysql_num_rows(res);
    // cout << "rows:" << rows << endl;

    /* 获取每一行的数据 */
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(res)) != NULL)
    {
        for (int idx = 0; idx < columns; idx++)
        {
            const char *tmp = NULL;
            if (row[idx] != NULL)
            {
                tmp = row[idx];
            }
            else
            {
                tmp = "NULL";
            }
            printf("%s\t", tmp);
        }
        printf("\n");
    }
    printf("\n");

    /* 关闭数据库 */
    mysql_close(conn);

    return 0;
}
#else
/* 测试回滚 */
int main()
{

    /* 初始化数据库句柄成功. */
    MYSQL *conn = mysql_init(NULL);
    if (conn == NULL)
    {
        cout << "mysql init failed." << endl;
        exit(-1);
    }

    /* 连接mysql服务器 */
    // const char *host = "127.0.0.1";
    const char *host = "localhost";
    const char *user = "root";
    const char *passwd = "1";
    const char *db = "musicDB";
    unsigned int port = 3306;
    if (mysql_real_connect(conn, host, user, passwd, db, port, NULL, 0) == NULL)
    {
        cout << "mysql connect error." << endl;
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }
    cout << "file:" << __FILE__ << ",lines:" << __LINE__ << ".success" << endl;

    /* 开启事务 */
    if (mysql_query(conn, "START TRANSACTION;"))
    {
        cout << "START TRANSACTION failed." << endl;
        /* 关闭数据库 */
        mysql_close(conn);
        /* 退出程序 */
        exit(-1);
    }

    const char *sql1 = "update account set money = money - 100 where username = 'zhangsan';";
    const char *sql2 = "drop table account where username = 'test';";
    // const char *sql2 = "update account set money = money + 100 where username = 'lisi';";

    if (mysql_query(conn, sql1))
    {
        cout << "sql1 execute error." << endl;
        /* 执行回滚 */
        mysql_query(conn, "ROLLBACK;");
        /* 关闭数据库 */
        mysql_close(conn);
        /* 退出程序 */
        exit(-1);
    }
    else
        cout << "sql1 execute success." << endl;

    if (mysql_query(conn, sql2))
    {
        cout << "sql2 execute error." << endl;
        /* 执行回滚 */
        mysql_query(conn, "ROLLBACK;");
        /* 关闭数据库 */
        mysql_close(conn);
        /* 退出程序 */
        exit(-1);
    }
    /* 提交事务 */
    mysql_query(conn, "COMMIT;");
    return 0;
}

#endif
